package com.wuwei.service.impl;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.utils.FallbackMethod;
import com.wuwei.service.PaymentService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.concurrent.TimeUnit;

/**
 * @description: PaymentServiceImpl <br>
 * @date: 2021/2/9 14:37 <br>
 * @author: wuwei666 <br>
 * @version: 1.0 <br>
 */
@Service
public class PaymentServiceImpl implements PaymentService {

    @Override
    public String payment_ok(Integer id) {
        return "当前线程" + Thread.currentThread().getName() + "id=" + id + "payment_ok";
    }

    /*@HystrixCommand(fallbackMethod = "fallback_Method",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
    })*/
    @Override
    public String payment_timeout(Integer id) {
        try {
            TimeUnit.MILLISECONDS.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "当前线程" + Thread.currentThread().getName() + "id=" + id + "payment_timeout";
    }

    @Override
    @HystrixCommand(fallbackMethod = "fallback_Breaker",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),//是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),//请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),//时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
    })
    public String payment_Breaker(Integer id){
        if (id < 0){
            throw new RuntimeException("不能小于0");
        }
        return Thread.currentThread().getName() + "---" + IdUtil.simpleUUID();
    }

    public String fallback_Breaker(Integer id){
        return "请求id不能小于0,当前线程" + Thread.currentThread().getName() + "当前id" + id;
    }

    public String fallback_Method(Integer id){
        return "请求超时,当前线程" + Thread.currentThread().getName();
    }


}
